package com.sx.pipeline.ui.examine.manager

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.res.ExamineInfoVo
import com.sx.pipeline.repos.http.res.ExamineOperatorVo
import com.sx.pipeline.repos.http.res.Ipage

class ExamineManagerVM(app: Application) : CYBaseViewModel(app) {

    fun getExamineTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_pending_review))
        tabs.add("已审核")
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        return tabs
    }

    fun getExamineTaskTabsIdList(): List<Int> {
        val tabs = mutableListOf<Int>()
        tabs.add(0)
        tabs.add(1)
        tabs.add(2)
        return tabs
    }

    val examineListEvent: SingleLiveEvent<Ipage<ExamineOperatorVo>> = SingleLiveEvent()
    fun getExamineList(page: Int, status: Int) {
        addSubscribe(
            mModel.getExamineOperatorList(status, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object :
                    CommonObserver<Ipage<ExamineOperatorVo>, BaseViewModel<*>>(this, true) {
                    override fun onResult(result: Ipage<ExamineOperatorVo>) {

                        examineListEvent.postValue(result)
                        if (page == 1) {
                            mUI.initRefreshEvent().postValue(true)
                        } else {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                })
        )
    }

    var examineInfoLiveData = SingleLiveEvent<ExamineInfoVo>()
    fun getExamineInfo(id: String) {
        addSubscribe(mModel.applyInfo(id)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<ExamineInfoVo, BaseViewModel<*>>(this, true) {
                override fun onResult(result: ExamineInfoVo) {
                    examineInfoLiveData.value=result
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            })
        )
    }

    var refreshEvent = SingleLiveEvent<Boolean>()
    fun optionAudit(id: Int,success:Boolean,remark: String?=""){
        addSubscribe(mModel.audit(id,if(success)1 else 2,remark)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any,BaseViewModel<*>>(this,true){
                override fun onResult(result: Any) {
                    ToastUtils.showShort("操作成功！")
                    refreshEvent.postValue(true)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort(msg)
                }

            }))
    }
}